package controller

import (
	"github.com/kataras/iris/v12"
	"log"
	"palms/model"
	"palms/service"
	"strconv"
	"time"
)

type TaskController struct {
	Service service.TaskService
}

func (c *TaskController) Get(ctx iris.Context) ([]model.Task, error) {
	pn := ctx.URLParam("pageNo")
	ps := ctx.URLParam("pageSize")
	var pageNo = 1
	var pageSize = 20
	var err error
	if ps != "" {
		pageNo, err = strconv.Atoi(pn)
		if err != nil {
			log.Println("pageNo参数错误[" + pn + "]")
		}
	}
	if ps != "" {
		pageSize, err = strconv.Atoi(ps)
		if err != nil {
			log.Println("pageSize参数错误[" + ps + "]")
		}
	}

	var task model.Task
	task.Name = ctx.URLParam("name")
	loc, _ := time.LoadLocation("Local")
	task.StartTime, err = time.ParseInLocation("2006-01-02 15:04:05", ctx.URLParam("startTime"), loc)
	if err != nil {
		log.Println("startTime参数错误[" + ps + "]")
	}
	task.EndTime, err = time.ParseInLocation("2006-01-02 15:04:05", ctx.URLParam("endTime"), loc)
	if err != nil {
		log.Println("startTime参数错误[" + ps + "]")
	}
	return c.Service.List(task, pageNo, pageSize)
}

func (c *TaskController) GetBy(id int64) (*model.Task, error) {
	return c.Service.Get(id)
}

func (c *TaskController) Post(task model.Task) (int64, error) {
	log.Println(time.Now())
	return c.Service.Create(task)
}

func (c *TaskController) Delete(id int64) error {
	return c.Service.Delete(id)
}
